﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
using System.Reflection;
using Newtonsoft.Json;
namespace E02
{   
    /// <summary>
    /// 返利
    /// </summary>  
    class A13 : ISYNChildGet
    {
        public void Code(ISYN handler, ref CodeResult result, SYNRight user, SerialType st, String baseNo, String billId)
        {
            try
            {
                result.Code = "E02.A13";
                List<E02A13M> ht = new List<E02A13M>();
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@FID", SqlDbType.NVarChar, 50);
                sps[0].Value = billId;
                string sql = " select FID BillID,FBillNo Number,case when FTime = '' then '1990-01-01 00:00:00' when FTime IS null then '1990-01-01 00:00:00' else FTime end BizDate,FBigText Note,FCheckBox3 Executed,b.FName Biller," +
                             " c.FName Checker,case when FDate = '' then '1990-01-01 00:00:00' when FDate IS null then '1990-01-01 00:00:00' else FDate end CheckDate " +
                             " ,(select FText5 from CardServer.dbo.KEW_DataBaseEntry where FNumber = " + baseNo + " ) FiUnitID from t_BOS_rebate a " +
                             " left join t_User b on a.FBiller = b.FUserID and a.FBiller <> 0 " +
                             " left join t_User c on a.FUser = c.FUserID and a.FUser <> 0 where a.FID = @FID";
                sql = sql + " select a.FInteger1 WBEntryID,a.FText1 WBNO,a.FText6 WBID,a.FText4 Customer,a.FText5 Product,a.FDecimal1 OrgPrice,a.FDecimal3 DesPrice,a.FText7 PolicyNo, " +
                            " a.FDecimal Num,a.FDecimal2 Amount,a.FDecimal9 Price,charindex('返利:' + d.FBillNo,b.FText13) NeedUpdate from t_BOS_rebateEntry a " +
                            " left join KEW_SalesInfoEntry b on b.FID = a.FText6 and b.fentryid = a.FInteger1 " +
                            " left join KEW_SalesInfo c on b.FID = c.FID " +
                            " left join t_BOS_rebate d on d.fid = a.fid" +
                            " where a.FID = @FID ";
                sql = sql + " select Ftext8 Customer,Fdecimal4 Num,Fdecimal5 Amount,Fdecimal7 returnMoney from t_BOS_rebateEntry2 where FID = @FID";

                Recordset rs = handler.Data.ExeProcedure(baseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows.Count == 1)
                    {
                        DataRow row = rs.Data.Tables[0].Rows[0];
                        E02A13M e02A13M = new E02A13M();
                        for (int i = 0; i < rs.Data.Tables[0].Columns.Count; i++)
                        {
                            DataColumn dc = rs.Data.Tables[0].Columns[i];
                            ReflectUtil.setValue(e02A13M, dc.ColumnName, row[dc.ColumnName].ToString());
                        }

                        List<E02A13P> detailedList = new List<E02A13P>();

                        foreach (DataRow entry in rs.Data.Tables[1].Rows)
                        {
                            E02A13P e02A13p = new E02A13P();
                            for (int j = 0; j < rs.Data.Tables[1].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[1].Columns[j];
                                ReflectUtil.setValue(e02A13p, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            detailedList.Add(e02A13p);
                        }
                        e02A13M.DetailedList = JsonConvert.SerializeObject(detailedList);

                        List<E02A13E> summaryList = new List<E02A13E>();

                        foreach (DataRow entry in rs.Data.Tables[2].Rows)
                        {
                            E02A13E e02A13e = new E02A13E();
                            for (int j = 0; j < rs.Data.Tables[2].Columns.Count; j++)
                            {
                                DataColumn dc = rs.Data.Tables[2].Columns[j];
                                ReflectUtil.setValue(e02A13e, dc.ColumnName, entry[dc.ColumnName].ToString());
                            }
                            summaryList.Add(e02A13e);
                        }
                        e02A13M.SummaryList = JsonConvert.SerializeObject(summaryList);
                        ht.Add(e02A13M);

                        result.Content = JsonConvert.SerializeObject(ht);
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("返利政策数据提取错误:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("返利政策数据提取错误:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 返利
    /// </summary>
    public class E02A13M
    {
        /// <summary>
        /// 返利单FID
        /// </summary>
        public Int32 BillID;
        /// <summary>
        /// 单据编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 业务日期
        /// </summary>
        public String BizDate;
        /// <summary>
        /// 备注
        /// </summary>
        public String Note;
        /// <summary>
        /// 已执行
        /// </summary>
        public Boolean Executed;
        /// <summary>
        /// 制单人
        /// </summary>
        public String Biller;
        /// <summary>
        /// 审核人
        /// </summary>
        public String Checker;
        /// <summary>
        /// 审核日期
        /// </summary>
        public String CheckDate;
        /// <summary>
        /// 返利明细列表
        /// </summary>
        public String DetailedList;
        /// <summary>
        /// 返利汇总列表
        /// </summary>
        public String SummaryList;

        /// <summary>
        /// EAS财务组织
        /// </summary>
        public String FiUnitID;
    }
    
    /// <summary>
    /// 返利明细
    /// </summary>
    public class E02A13P
    {
        /// <summary>
        /// 客户内码
        /// </summary>
        public String Customer;
        /// <summary>
        /// 产品
        /// </summary>
        public String Product;
        /// <summary>
        /// 调整前单价
        /// </summary>
        public Decimal OrgPrice;
        /// <summary>
        /// 调整后单价
        /// </summary>
        public Decimal DesPrice;
        /// <summary>
        /// 过磅单ID
        /// </summary>
        public Int32 WBID;
        /// <summary>
        /// 过磅单号
        /// </summary>
        public String WBNO;
        /// <summary>
        /// 过磅单分录ID
        /// </summary>
        public Int32 WBEntryID;
        /// <summary>
        /// 政策编号
        /// </summary>
        public String PolicyNo;
        /// <summary>
        /// 数量
        /// </summary>
        public Decimal Num;
        /// <summary>
        /// 金额
        /// </summary>
        public Decimal Amount;
        /// <summary>
        /// 匹配单价
        /// </summary>
        public Decimal Price;
        /// <summary>
        /// 需要更新过磅单
        /// </summary>
        public Int32 NeedUpdate;

    }


    /// <summary>
    /// 返利汇总
    /// </summary>
    public class E02A13E
    {
        /// <summary>
        /// 客户内码
        /// </summary>
        public String Customer;
        /// <summary>
        /// 数量
        /// </summary>
        public Decimal Num;
        /// <summary>
        /// 金额
        /// </summary>
        public Decimal Amount;
        /// <summary>
        /// 返利金额
        /// </summary>
        public Decimal returnMoney;

    }


























}
